package com.aier.common.common;

import java.util.Calendar;

public class CalculateBabyAge {
    
    public static String getBabyAge(String birthday,String nowday) {
        String age = "";
        boolean todayBirth = false;
        int day = 0;
        int y = 0;
        int m = 0;
        int d = 0;
        
        String[] time = birthday.split("-");
        y = Integer.parseInt(time[0]);
        m = Integer.parseInt(time[1]);
        d = Integer.parseInt(time[2]);

        // 获取当前年月日与生日比较的差
        Calendar selectDate = Calendar.getInstance();
        selectDate.set(Calendar.YEAR, y);
        selectDate.set(Calendar.MONTH, m - 1);
        selectDate.set(Calendar.DAY_OF_MONTH, d);
        Calendar currentDate = Calendar.getInstance();
        currentDate.set(Calendar.YEAR, Integer.parseInt(nowday.split("-")[0]));
        currentDate.set(Calendar.MONTH, Integer.parseInt(nowday.split("-")[1]) - 1);
        currentDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(nowday.split("-")[2]));
        
        int lastMonth=(currentDate.get(Calendar.MONTH)+1)-1; 
        int years = currentDate.get(Calendar.YEAR) - selectDate.get(Calendar.YEAR);
        int months = currentDate.get(Calendar.MONTH) - selectDate.get(Calendar.MONTH);
        int days = currentDate.get(Calendar.DAY_OF_MONTH)- selectDate.get(Calendar.DAY_OF_MONTH);
        // 生日晚于当前日期，则直接返回空
        boolean flag = years==0&&months==0&&days<0;
        if (years<0 || (years==0&&months<0) || flag) {
            return age;
        }
        if (days < 0) {
            months = months - 1;
            switch (lastMonth) {
                case 1:
                case Constant.num_3:
                case Constant.num_5:
                case Constant.num_7:
                case Constant.num_8:
                case Constant.num_10:
                case Constant.num_12:
                    day = Constant.num_31;
                    break;
                case Constant.num_4:
                case Constant.num_6:
                case Constant.num_9:
                case Constant.num_11:
                    day = Constant.num_30;
                    break;
                default:
                    int year = currentDate.get(Calendar.YEAR);
                    // 平年 闰年
                    if ((year % Constant.num_4 == 0 && year % Constant.num_100 != 0) || year % Constant.num_400 == 0) {
                        day = Constant.num_29;
                    } else {
                        day = Constant.num_28;
                    }
                    break;
            }
            days = days + day;
        }
        if (months < 0) {
            years = years - 1;
            months = months + Constant.num_12;
        }
        if (years < 0) {
            years = 0;
        }
        if(years==0){
            if(months==0){
                if(days==0){
                    age="今天是宝宝的出生日期";
                    todayBirth = true;
                }else{
                    age=days + "天";
                }
            }else{
                if(days==0){
                    age=months + "个月";
                }else{
                    age =months + "个月又" + days + "天";
                }
            }
        }else{
            if(months==0){
                if(days==0){
                    age = years + "岁";
                }else{
                    age = years + "岁又" + days + "天";
                }
            }else{
                if(days==0){
                    age = years + "岁" + months + "个月";
                }else{
                    age = years + "岁" + months + "个月又" + days + "天";
                }
                
            }
        }
        if (!todayBirth) {
            age = "您的宝宝"+age+"了";
        }
        return age;
    }

}
